package global

import (
	"MoSkeleton/config/properties"
	"MoSkeleton/framework"
	"MoSkeleton/framework/core"
	"MoSkeleton/framework/core/config"
	properties2 "MoSkeleton/framework/core/config/properties"
	"MoSkeleton/framework/mo_gin"
	"MoSkeleton/framework/mo_gorm"
	"MoSkeleton/framework/utils"
	base2 "MoSkeleton/models/base"
	"MoSkeleton/services/base"
	"MoSkeleton/web/routers/external_type"
)

type ConfigContainer struct {
	BootstrapConfigOper  config.ConfigOper
	BootstrapConfig      *properties2.BootstrapProperties
	AppConfigOper        config.ConfigOper
	AppConfig            *properties.App
	AppStorageConfigOper config.ConfigOper
	AppStorageConfig     *properties.AppStorage
}

type ConfigEvent struct {
}

func (c *ConfigEvent) OnInitSystem() error {
	framework.ConfigContainerInst.BootstrapConfigOper = ConfigContainerInst.BootstrapConfigOper
	framework.ConfigContainerInst.BootstrapConfig = ConfigContainerInst.BootstrapConfig
	framework.InitContainerInst.Register(core.InitSys_Order+100, &base2.InitModels{})
	framework.InitContainerInst.Register(core.InitSys_Order+101, &base.InitServices{})
	framework.InitContainerInst.Register(core.InitSys_Order+200, external_type.NewInitRouter(ConfigContainerInst.AppConfig.App.Gin))
	framework.InitContainerInst.Register(core.InitSys_Order+280, &CommonInitServices{})
	return nil
}

func (c *ConfigEvent) OnBeforeInitSystem() error {
	if err := c.initBootstrapConfig(); err != nil {
		return err
	}
	if err := c.initAppConfig(); err != nil {
		return err
	}
	mo_gorm.PersistentDb = &ConfigContainerInst.AppStorageConfig.App.PersistentDb
	framework.RedisProperties = &ConfigContainerInst.AppStorageConfig.App.RedisProperty
	framework.SnowflakeProperty = &ConfigContainerInst.AppConfig.App.Snowflake
	mo_gin.CommonGin = &ConfigContainerInst.AppConfig.App.Gin
	utils.CommonGin = &ConfigContainerInst.AppConfig.App.Gin
	utils.SecurityPropertyInst = &ConfigContainerInst.AppConfig.App.Security
	if mo_gin.CommonGin.Mode == "" {
		mo_gin.CommonGin.Mode = ConfigContainerInst.BootstrapConfig.Sys.Profile.Mode
	}

	return nil
}

func (c *ConfigEvent) OnAfterInitSystem() error {
	return nil
}

func (c *ConfigEvent) initBootstrapConfig() error {
	ConfigContainerInst.BootstrapConfigOper = config.ConfigOperManagerLoaderInst.LoadBootstrapConfig()
	ConfigContainerInst.BootstrapConfig = new(properties2.BootstrapProperties)
	if err := ConfigContainerInst.BootstrapConfigOper.Unmarshal(ConfigContainerInst.BootstrapConfig); err != nil {
		return err
	}
	framework.Profile = &ConfigContainerInst.BootstrapConfig.Sys.Profile
	return nil
}
func (c *ConfigEvent) initAppConfig() error {
	ConfigContainerInst.AppConfigOper = config.ConfigOperManagerLoaderInst.LoadApplicationConfig(ConfigContainerInst.BootstrapConfig)
	ConfigContainerInst.AppConfig = new(properties.App)
	if err := ConfigContainerInst.AppConfigOper.Unmarshal(ConfigContainerInst.AppConfig); err != nil {
		return err
	}

	ConfigContainerInst.AppStorageConfigOper = config.ConfigOperManagerLoaderInst.LoadApplicationStorageConfig(ConfigContainerInst.BootstrapConfig)
	ConfigContainerInst.AppStorageConfig = new(properties.AppStorage)
	if err := ConfigContainerInst.AppStorageConfigOper.Unmarshal(ConfigContainerInst.AppStorageConfig); err != nil {
		return err
	}
	return nil
}
